package com.outstanding.his.doctor.facade;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.outstanding.his.doctor.domain.Doctor;
import com.outstanding.his.doctor.service.IDoctorService;

@Controller
@SessionAttributes("doctor")
public class DoctorController {

	private IDoctorService doctorService;

	public void setDoctorService(IDoctorService doctorService) {
		this.doctorService = doctorService;
	}

	@RequestMapping("/doctor/list.do")
	public String doctorList(Model model) {
		model.addAttribute("doctors", doctorService.getDoctorList());
		return "/doctor/list";
	}

	@RequestMapping("/doctor/new.do")
	public String doctorNewForm(Model model) {
		Doctor doctor = new Doctor();
		model.addAttribute("doctor", doctor);
		return "/doctor/new";
	}

	@RequestMapping(value = "/doctor/submit", method = RequestMethod.POST)
	public String doctorSubmit(@ModelAttribute("doctor") Doctor doctor,
			Model model) {
		if (doctor.getDoctorId() != null) {
			doctor = doctorService.updateDoctor(doctor);
		} else {
			doctor = doctorService.submitDoctor(doctor);
		}
		return "redirect:/doctor/list.do";
	}

	@RequestMapping(value = "/doctor/view.do")
	public String doctorView(@RequestParam("doctorId") Integer doctorId,
			Model model) {
		Doctor doctor = doctorService.getDoctor(doctorId);
		model.addAttribute(doctor);
		return "/doctor/view";
	}

	@RequestMapping(value = "/doctor/edit.do")
	public String doctorEdit(@RequestParam("doctorId") Integer doctorId,
			Model model) {
		Doctor doctor = doctorService.getDoctor(doctorId);
		model.addAttribute(doctor);
		return "/doctor/edit";
	}

	/*
	 * @RequestMapping("/doctor/select.do") public String doctorSelect(Model
	 * model) { model.addAttribute("doctors", doctorService.getDoctorList());
	 * return "/doctor/select.jsp"; }
	 */

	@RequestMapping(value = "/doctor/login", method = RequestMethod.POST)
	public String doctorLogin(
			@RequestParam("doctorLoginName") String doctorLoginName,
			@RequestParam("doctorPassword") String doctorPassword, Model model,
			HttpServletRequest request) {
		Doctor doctor = new Doctor();
		doctor.setDoctorLoginName(doctorLoginName);
		doctor.setDoctorPassword(doctorPassword);
		doctor = doctorService.loginDoctor(doctor);
		if (doctor != null) {
			model.addAttribute(doctor);
			request.getSession().setAttribute("user", doctor);
		} else {
			return "redirect:/login.jsp";
		}
		return "redirect:/patient/new.do";
	}

	@RequestMapping(value = "/logout")
	public String doctorLogout(HttpServletRequest request) {
		request.getSession().removeAttribute("user");
		return "redirect:/login.jsp";
	}

}
